function formatInteger( integer, pattern )
{
	var result = '';

	integerIndex = integer.length - 1;
	patternIndex = pattern.length - 1;

	while ( (integerIndex >= 0) && (patternIndex >= 0) )
	{
		var digit = integer.charAt( integerIndex );
		integerIndex--;
		
		// Skip non-digits from the source integer (eradicate current formatting).
		if ( (digit < '0') || (digit > '9') )  continue;
	
		// Got a digit from the integer, now plug it into the pattern.
		while ( patternIndex >= 0 )
		{
			var patternChar = pattern.charAt( patternIndex );
			patternIndex--;
			
			// Substitute digits for '#' chars, treat other chars literally.
			if ( patternChar == '#' )
			{
				result = digit + result;
				break;
			}
			else
			{
				result = patternChar + result;
			}
		}
	}
	
	return result;
}
function leftTrim(s)
{
	for (var i=0; i < s.length; i++) {
		if (s.charAt(i) != ' ') break;
	}
	if (i == 0) return s;
	return s.substring(i);
}

function rightTrim(s)
{
	for (var i=s.length-1; i >= 0; i--) {
		if (s.charAt(i) != ' ') break;
	}
	if (i == s.length-1) return s;
	return s.substring(0,i+1);
}

function trim(s)
{
	return leftTrim(rightTrim(s));
}

function between(x,y,z)
{
	return ((x >= y) && (x <= z));
}


function isNumeric(val)
{
	val = "" + val;
	for (var i = 0; i < val.length; i++) {
		var ch = val.charAt(i);
		if (!between(ch,"0","9")) {
			if (ch != "." && ch != "-" && ch != "e") {
				return false;
			}
		}
	}
	return true;
}

function getObj(id)
{
	var obj = document.getElementById(id);
	if (obj == null) {
		//alert("Object id = " + id + " is null.");
	}
	return obj;
}

function setObjColor(obj,value)
{
	if (obj != null && value != null) obj.style.color = value;
}

function setColor(id,value)
{
	setObjColor(getObj(id),value);
}

function isObjVisibled(obj)
{
	if (obj == null) return false;
	return (obj.style.display != "none");
}

function isVisibled(id)
{
	return isObjVisibled(getObj(id));
}

function showObj(obj,sh)
{
	if (obj == null) return;
	if (sh == null) sh = true;
	obj.style.display = sh ? "" : "none";
}


function hideObj(obj)
{
	showObj(obj,false);
}

function show(id,sh)
{
	showObj(getObj(id),sh);
}

function hide(id)
{
	hideObj(getObj(id));
}
	
function setFocus(id)
{
	var obj = getObj(id);
	if (isObjVisibled(obj)) obj.focus();
}

function keyboard(code)
{
  return (window.event && window.event.keyCode == code);
}

function clickChange(show_obj,hide_obj)
{ 
	show(show_obj);
	hide(hide_obj);
}


function setSelected(id,value)
{
	setValue(id,value);
}

function getSelectedObj(id)
{
	var obj = getObj(id);
	if (obj == null) return null;
	var	idx = obj.selectedIndex;
	return obj.options[idx];
}

function getSelectedValue(id)
{
	var obj = getSelectedObj(id);
	if (obj == null) return 0;
	return obj.value;
}

function getSelectedText(id)
{
	var obj = getSelectedObj(id);
	if (obj == null) return 0;
	return obj.text;
}

function getSelectedId(id)
{
	var obj = getSelectedObj(id);
	if (obj == null) return 0;
	return obj.id;
}

function setObjValue(obj,value)
{
	if (obj != null) obj.value = value;
}

function setValue(id,value)
{
	var obj = setObjValue(getObj(id),value);
}

function getValue(id)
{
	var obj = getObj(id);
	if (obj != null) return obj.value;
	else return 0;
}

function setText(id,value)
{
	var obj = getObj(id);
	if (obj != null && value != null) obj.innerText = value;
}

function getText(id)
{
	var obj = getObj(id);
	if (obj != null) return obj.innerText;
	else return "";
}

function setChecked(id,value)
{
	var obj = getObj(id);
	if (obj == null) return;
	if (value == null) value = true;
	obj.checked = value;
}

var old_row_obj = null;

function highlightOn(row_obj) 
{
	if (row_obj == null) return;
	if (old_row_obj != null) old_row_obj.bgColor = row_obj.bgColor;//"#FAFBF5";
	old_row_obj = row_obj;
	row_obj.bgColor = "#E7E5C4";	
}

function dblClick(row_obj) 
{
	highlightOn(row_obj);
	clickCardView(row_obj);	
	//formMain.submit();
}

function getDay(date)
{
	return parseInt(date.substring(0, date.indexOf("-")),10);
}

var Months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","");

function getNewDate()
{
	var date = new Date();
	var dd   = date.getDate();
	var mon  = Months[date.getMonth()];
	var yy   = date.getYear();
	yy += (yy < 100) ? 1900 : 0
	yy %= 100;
	var day  = (dd < 10 ? "0" : "") + dd;
	var year = (yy < 10 ? "0" : "") + yy;
	return day + "-" + mon + "-" + year;
}

function get_NewDate()
{
	var date = new Date();
	var dd   = date.getDate();
	var mm  = date.getMonth();
	var yy   = date.getYear();
	yy += (yy < 100) ? 1900 : 0
	
	var day  = (dd < 10 ? "0" : "") + dd;
	var mon  = (mm < 10 ? "0" : "") + mm;
	var year = "" + yy;
	
	return year + "-" + mon + "-" + day;
}

function getNewTime()
{
	var date = new Date();
	var hh   = date.getHours();
	var mm   = date.getMinutes();
	var hour = (hh < 9 ? "0" : "") + hh;
	var minu = (mm < 9 ? "0" : "") + mm;
	return hour + ":" + minu;
}

function get_NewTime()
{
	var date = new Date();
	var hh   = date.getHours();
	var mm   = date.getMinutes();
	var hour = (hh < 9 ? "0" : "") + hh;
	var minu = (mm < 9 ? "0" : "") + mm;
	return hour + ":" + minu;
}

function getMonth(date)
{
	var mon = date.substring(date.indexOf("-")+1, date.lastIndexOf("-"));	
	for (var i=0; Months[i] != ""; i++) {
		if (Months[i] == mon) return i+1;
	}
	return 0;
}

function getYear(date)
{
	return parseInt(date.substring(date.lastIndexOf("-")+1, date.length),10);	
}

function dateCompare(date1,date2)
{
	//if (date1 == "" || date2 == "") return true;
	var dd1 = getDay(date1);
	var mm1 = getMonth(date1);	
	var yy1 = getYear(date1);
	
	var dd2 = getDay(date2);
	var mm2 = getMonth(date2);	
	var yy2 = getYear(date2);

	if (yy1 > yy2) return 1;
	if (yy1 < yy2) return -1;

	if (mm1 > mm2) return 1;
	if (mm1 < mm2) return -1;

	if (dd1 > dd2) return 1;
	if (dd1 < dd2) return -1;
	
	return 0;
}	

function addNewListItem(list,new_item)
{
	if (list == null) {
		alert("List Object is null.");
		return -1;
	}
	if (list.value < 0) return -1;
	var newop = new Option("text",-1, true, true);
	newop.text = new_item;
	list.options[list.length] = newop;
	list.selectedIndex = list.length-1;

	return list.length-1;
}

function addNewListItemById(id,new_item)
{
	return addNewListItem(getObj(id),new_item);
}

function addZero(num,len)
{
	var n = 1;
	for (var i = 0; i < len; i++) n *= 10;
	n += parseInt(num);
	return ("" + n).substring(1);
}

function formatNumber(number,dec,sep)
{
	valid = false;
	var pre = "";	
	for (var i=0; i < dec; i++) pre += "0";
	
	if (number == null) return "0." + pre;
	if (number == "") return "0." + pre;
	
	if (!isNumeric(number)) {
		alert("Invalid input number " + number);
		return formatNumber(0,dec,sep);
	}
	
    var numberin = "" + parseFloat(number);
	
	if(numberin.length == 0) {
		return "0." + pre;
	}

	var index = numberin.indexOf('.');
	var num = numberin;	
	
	if (index >= 0) 
	{
		num = numberin.substr(0, index);
		pre  = numberin.substr(index+1, dec);
		var len = dec-pre.length;
		for (var i = 0; i < len; i++) pre += "0";
	}
	if (!sep) return (dec > 0) ? (num + "." + pre) : num;
	var j = num.length - 3;
	var s = "";
	while (j > 0)
	{
		s = "," + num.substr(j,3) + s;
		j = j - 3;
	}
	s = num.substr(0,j+3) + s;
	return s + "." + pre;
}

function formatNumberObj(numberobj,dec,sep)
{
	numberobj.value = formatNumber(numberobj.value,dec,sep);
}

function formatNumberById(id,dec,sep)
{
	var obj = getObj(id);
	if (obj != null) formatNumberObj(obj,dec,sep);
}

function formatPercentNumber(nObj)
{
	formatNumberObj(nObj,2,false);
}

function changeUnit()
{
	var form = document.formMain;
	form.units.value = getSelectedId("unit_id");
}

function toFloat(str)
{
	if (str == null) return 0;
	str = "" +  str;
	if (str == "") return 0;
	if (!isNumeric(str)) {
		alert("Invalid input number " + str);
		return 0;
	}
	return parseFloat(str);
}

